package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/*
*
* 权限管理服务
*
* */
@Service(interfaceClass = RightsManagementService.class)
@Transactional
public class RightsManagementServiceImpl implements RightsManagementService {
    @Resource
    private PermissionDao permissionDao;


    //添加权限
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    //分页查询
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        //完成分页查询，mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page = permissionDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据id删除权限
    public void delete(Integer id) {
        //先删除中间表数据
        permissionDao.deleteByPermissionId(id);
        //删除数据
        permissionDao.deleteById(id);

    }

    //编辑权限
    public void edit(Permission permission) {

        permissionDao.edit(permission);
    }

    //根据ID查询权限
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    //查询所有
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

}
